/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package prototype.runtime.impl.unit;

import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import jess.JessException;
import prototype.persistence.pojo.Position;
import prototype.persistence.pojo.Scenario;
import prototype.persistence.pojo.User;
import prototype.persistence.pojo.UserHasUnit;
import prototype.runtime.impl.order.FireOrder;
import prototype.runtime.impl.order.MoveOrder;
import prototype.runtime.interfaces.Order;
import prototype.runtime.interfaces.RuntimeUnit;

/**
 *
 * @author Server
 */
public class GenericUnit implements RuntimeUnit {

    private UserHasUnit unit;
    private boolean AI = false;
    private Order order = null;
    private List<Order> availableOrders = new ArrayList<Order>();
    private JESSHelper jessEngine = null;

    public GenericUnit() {
        availableOrders.add(new FireOrder());
        availableOrders.add(new MoveOrder());
    }

    public UserHasUnit getUnit() {
        return unit;
    }

    public void setUnit(UserHasUnit uhu) {
        this.unit = uhu;
    }

    public List<Order> getOrderList() {
        return availableOrders;
    }

    public Order executeOrder(Order order) {
        if (order != null) {
            this.order = order;
        }
        return this.order;
    }

    public void assignToAI() {
        AI = true;
    }

    public void regainFromAI() {
        AI = false;
    }

    public boolean isAI() {
        return AI;
    }

    public void cancelOrder() {
        order = null;
    }

    public boolean hasOrder() {
        return order != null;
    }

    public Order getAIOrder(Scenario scenario) {

        List<UserHasUnit> uhus = new ArrayList<UserHasUnit>();
        for (User u : scenario.getUsers()) {
            for (UserHasUnit uhu : u.getUserHasUnits()) {
                if (uhu.getUnit().getHealth() > 0) {
                    uhus.add(uhu);
                }
            }
        }
        jessEngine = new JESSHelper(uhus, unit);

        FireOrder fireOrder = new FireOrder();
        try {
            Position pos = jessEngine.run();
            if (pos == null) {
                return null;
            }
            //applyRandomization(pos);
            fireOrder.setPosition(pos);
        } catch (JessException ex) {
            Logger.getLogger(GenericUnit.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
        return fireOrder;
    }

    private void applyRandomization(Position pos) {
        double accuracyRadius,randX,randZ;

        accuracyRadius = (100 - unit.getUnit().getAccuracy()) * (distance(unit.getPosition(),pos)) * 0.005;
        System.out.println("accRad:" + accuracyRadius);
        
        randX = Math.random();
        pos.setX(pos.getX() + (int)(accuracyRadius * randX));
        randZ = Math.random();
        pos.setZ(pos.getZ() + (int)(accuracyRadius * randZ));
    }

    private int distance(Position position, Position pos) {
        double length;

        length = Math.pow(pos.getX() - position.getX(), 2);
        length += Math.pow(pos.getY() - position.getY(), 2);
        length += Math.pow(pos.getZ() - position.getZ(), 2);

        return (int) Math.sqrt(length);
    }


}
